﻿using System;
using System.Windows.Forms;
using System.Drawing;
using Models;
using VirtualGameBLL;
using System.Collections.Generic;

namespace VirtualGame
{
    public partial class FormMain : Form
    {
        private  List<Role> friends;
        private  List<Role> enemies;
        public FormMain()
        {
            InitializeComponent();
            FormLogin formLogin = new FormLogin();
            formLogin.ShowDialog();

            friends = RoleManager.GetAllFriends();
            enemies = RoleManager.GetAllEnemies();

            RoleUI roleUI;
            foreach(Role role in enemies) {
                roleUI = NewRoleUI(role);
                role.RoleUI = roleUI;
            }
            foreach (Role role in friends)  {
                roleUI = NewRoleUI(role);
                role.RoleUI = roleUI;
            }
            if(enemies.Count>=0&&friends.Count>=0)
                if(enemies[0].Speed> friends[0].Speed)
                {
                    TeamAtk(enemies, friends);
                }
        }

        public void TeamAtk(List<Role> atkers, List<Role> defenders)
        {
            foreach (Role atker in atkers)
            {
                RoleMove(atker.RoleUI, defenders[0].CurrentLocation);
                RoleMove(atker.RoleUI, atker.OriginaLocation);
                ChangeBloodRatio(defenders[0].RoleUI, RoleManager.Atk(atker, defenders[0]));
                if (defenders[0].CurrentLife == 0) {
                    destroyRoleUI(defenders[0].RoleUI);
                    RoleManager.DeleteRoleById(defenders[0].Id);
                    defenders.Remove(defenders[0]);
                }
                if (defenders.Count == 0) {
                    if(atkers[0].Type== 0 || atkers[0].Type == 1)  {
                        MessageBox.Show("You Win!");
                    } else  {
                        MessageBox.Show("You Loss!");
                    }
                    return;
                }
            }
        }

        private RoleUI NewRoleUI(Role role)
        {
            Image image = (Image)Properties.Resources.ResourceManager.GetObject("r" + role.Id);
            Size size = image.Size;
            PictureBox pictureBoxhero;
            pictureBoxhero = new PictureBox();

            Label labelRed;
            labelRed = new Label();

            Label labelYellow;
            labelYellow = new Label();

            PictureBox pictureBoxWeapon;
            pictureBoxWeapon = new PictureBox();

            RoleUI roleUI = new RoleUI(pictureBoxhero,labelRed,labelYellow,pictureBoxWeapon);
            
            // pictureBoxhero
            ((System.ComponentModel.ISupportInitialize)(pictureBoxhero)).BeginInit();
            pictureBoxhero.BackColor = Color.Transparent;
            pictureBoxhero.Image = image;
            pictureBoxhero.Name = role.Name;
            pictureBoxhero.Size = size;
            pictureBoxhero.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBoxhero.TabIndex = 0;
            pictureBoxhero.TabStop = false;

            // labelRed
            labelRed.BackColor = Color.Red;
            
            labelRed.Name = "labelRed";
            labelRed.Size = new Size(size.Width, 15);
            labelRed.TabIndex = 1;

            // labelYellow
            labelYellow.BackColor = Color.Yellow;
            
            labelYellow.Name = "labelYellow";
            labelYellow.Size = new Size((int)(size.Width* 1.0 * role.CurrentLife / role.OriginalLife), 15);
            labelYellow.TabIndex = 2;

            // pictureBoxWeapon
            ((System.ComponentModel.ISupportInitialize)(pictureBoxWeapon)).BeginInit();
            pictureBoxWeapon.BackColor = Color.Transparent;
            pictureBoxWeapon.Image = (Image)Properties.Resources.ResourceManager.GetObject("w" + role.Wpn.Id);

            pictureBoxWeapon.Name = "pictureBoxWeapon";
            pictureBoxWeapon.Size = new Size(60, 60);
            pictureBoxWeapon.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBoxWeapon.TabIndex = 3;
            pictureBoxWeapon.TabStop = false;

            pictureBoxhero.Location = role.CurrentLocation;
            labelRed.Location = new Point(role.CurrentLocation.X, role.CurrentLocation.Y - 15);
            labelYellow.Location = new Point(role.CurrentLocation.X, role.CurrentLocation.Y - 15);
            pictureBoxWeapon.Location = new Point(role.CurrentLocation.X, role.CurrentLocation.Y - 60 - 15);

            roleUI.LabelRed.Visible = true;
            roleUI.LabelYellow.Visible = true;


            Controls.Add(pictureBoxWeapon);
            Controls.Add(labelYellow);
            Controls.Add(labelRed);
            Controls.Add(pictureBoxhero);
            ((System.ComponentModel.ISupportInitialize)(pictureBoxWeapon)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(pictureBoxhero)).EndInit();
            return roleUI;
        }
       
        private void destroyRoleUI(RoleUI roleUI)
        {
            Controls.Remove(roleUI.PictureBoxWeapon);
            Controls.Remove(roleUI.PictureBoxhero);
            Controls.Remove(roleUI.LabelRed);
            Controls.Remove(roleUI.LabelYellow);
            roleUI.PictureBoxWeapon.Dispose();
            roleUI.PictureBoxhero.Dispose();
            roleUI.LabelRed.Dispose();
            roleUI.LabelYellow.Dispose();
        }

        // 延时效果
        public void Delay(int times)
        {
            for (int i = 0; i < times; i++)
                Application.DoEvents();
        }
        public void RoleMove(RoleUI roleUI, Point location)
        {  
            int originaX = roleUI.PictureBoxhero.Location.X;
            int originaY = roleUI.PictureBoxhero.Location.Y;
            int diffX = (location.X - originaX) / 10;
            int diffY = (location.Y - originaY) / 10;
            if (roleUI.PictureBoxWeapon != null)
            {
                originaX = roleUI.PictureBoxWeapon.Location.X;
                originaY = roleUI.PictureBoxWeapon.Location.Y;

                for (int i = 1; i <= 10; i++) {
                    roleUI.PictureBoxWeapon.Location = new Point(originaX + diffX * i, originaY + diffY * i);
                    Delay(20000);
                }
                roleUI.PictureBoxWeapon.Location = location;
            }
            else
            {
                for (int i = 1; i <= 10; i++)
                {
                    roleUI.PictureBoxhero.Location = new Point(roleUI.PictureBoxhero.Location.X + diffX,
                        roleUI.PictureBoxhero.Location.Y + diffY);

                    roleUI.LabelRed.Location = new Point(roleUI.LabelRed.Location.X + diffX,
                        roleUI.LabelRed.Location.Y + diffY);

                    roleUI.LabelYellow.Location = new Point(roleUI.LabelYellow.Location.X + diffX,
                        roleUI.LabelYellow.Location.Y + diffY);

                    roleUI.PictureBoxWeapon.Location = new Point(roleUI.PictureBoxWeapon.Location.X + diffX,
                        roleUI.PictureBoxWeapon.Location.Y + diffY);
                    Delay(20000);
                }

                roleUI.PictureBoxhero.Location = location;
                roleUI.LabelRed.Location = new Point(roleUI.PictureBoxhero.Location.X, roleUI.PictureBoxhero.Location.Y - 15);
                roleUI.LabelYellow.Location = new Point(roleUI.PictureBoxhero.Location.X, roleUI.PictureBoxhero.Location.Y - 15);
                roleUI.PictureBoxWeapon.Location = new Point(roleUI.PictureBoxhero.Location.X, roleUI.PictureBoxhero.Location.Y - 60 - 15);
            }
            roleUI.LabelRed.Visible = true;
            roleUI.LabelYellow.Visible = true;
        }

        public void ChangeBloodRatio(RoleUI roleUI,double bloodRatio)
        {
            roleUI.LabelYellow.Size = new Size((int)(roleUI.LabelRed.Size.Width * bloodRatio), 15);
            roleUI.LabelRed.Visible = true;
            roleUI.LabelYellow.Visible = true;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void picBoxAtk_Click(object sender, EventArgs e)
        {
            TeamAtk(friends, enemies);
            TeamAtk(enemies, friends);
        }
    }
}
